#include "Health.h"

Health::Health()
	: mMaxHealth (0)
	, mCurrentHealth (0)
	, mCurrentSprite (nullptr)
{}

Health::Health(int maxH)
	: mMaxHealth (maxH)
	, mCurrentHealth (0)
	, mCurrentSprite (nullptr)
{
}

Health::~Health()
{
}

void Health::Load()
{
	mCurrentHealth = mMaxHealth;
	mSprites[0].Load("healthbar0.png");
	mSprites[1].Load("healthbar1.png");
	mSprites[2].Load("healthbar2.png");
	mSprites[3].Load("healthbar3.png");
	mSprites[4].Load("healthbar4.png");
	mSprites[5].Load("healthbar5.png");
}

void Health::Unload()
{
	for(int i=0; i<6; ++i)
		mSprites[i].Unload();
}

void Health::Update(float deltaTime, SVector2 pos)
{
	switch(mCurrentHealth)
	{
		case 0:
			mCurrentSprite = &mSprites[0];
			break;
		case 1:
			mCurrentSprite = &mSprites[1]; 
			break;
		case 2:
			mCurrentSprite = &mSprites[2];
			break;
		case 3:
			mCurrentSprite = &mSprites[3];
			break;
		case 4:
			mCurrentSprite = &mSprites[4];
			break;
		case 5:
			mCurrentSprite = &mSprites[5];
			break;
	}
	mPosition = pos;
}

void Health::Render(const SVector2& viewOffset)
{
	int width = mCurrentSprite -> GetWidth();
	int height = mCurrentSprite -> GetHeight();

	SVector2 offset(width * 0.5f, height * 0.5f);
	SVector2 renderPosition = mPosition - offset - viewOffset;

	mCurrentSprite -> SetPosition(renderPosition);
	mCurrentSprite -> Render();
}

void Health::SetCurrentHealth(int health)
{
	mCurrentHealth = health;
}

void Health::TakeDamage(int amount)
{
	mCurrentHealth -= amount;
}

bool Health::IsAlive()
{
	return (mCurrentHealth > 0);
}

int Health::GetCurrentHealth()
{
	return mCurrentHealth;
}

void Health::ScaleSprite(float x, float y, bool aboutCenter)
{
	for(int i=0; i<6; ++i)
		mSprites[i].SetScale(x, y, aboutCenter);
}